﻿using Architecture.Repository.Interface;
using Architeture.Domain;
using Architeture.Domain.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service
{
    public class AFLservice : IAFLservice
    {
        protected readonly IAFLRepository aflRep;
        private readonly IUserRepository userRep;

        public AFLservice(IAFLRepository aflRep, IUserRepository userRep)
        {
            this.aflRep = aflRep;
            this.userRep = userRep;
        }

        public async Task<int> AFLadd(Askforleave a)
        {
            a.AddTime = DateTime.Now;
            a.State = 0;
            return await aflRep.AddAsync(a);
        }

        public async Task<int> ASKDel(int id)
        {
            return await aflRep.Delete(id);
        }

        public async Task<int> ASKupd(Askforleave a)
        {
            return await aflRep.Update(a);
        }

        public async Task<AskPageDto> GetASKInfo(int uid, int pageindex, int pagesize, string? edate, string? odate, string? uname, int? state)
        {
            var size = (pageindex - 1) * pagesize;
            var userinfo = await userRep.GetModelAsync(uid);
            var list = (await aflRep.GetAllAsync()).Where(x => x.UserName == userinfo.UserName);
            if (userinfo.IsBoos == true)
            {
                var newlist = (await aflRep.GetAllAsync()).Where(x => x.department == userinfo.department).ToList();
                if (edate != null)
                {
                    newlist = newlist.Where(x => x.AddTime >= Convert.ToDateTime(edate).AddDays(1)).ToList();
                }
                if (odate != null)
                {
                    newlist = newlist.Where(x => x.AddTime < Convert.ToDateTime(odate).AddDays(1)).ToList();
                }
                if (edate != null && odate != null)
                {
                    newlist = newlist.Where(x => x.AddTime < Convert.ToDateTime(odate).AddDays(1) && x.AddTime >= Convert.ToDateTime(edate).AddDays(1)).ToList();
                }
                if (uname != null)
                {
                    newlist = newlist.Where(x => x.UserName.Contains(uname)).ToList();
                }
                if (state != null)
                {
                    newlist = newlist.Where(x => x.State == state).ToList();
                }
                var page = new AskPageDto();
                page.pagecount = newlist.Count();
                page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / pagesize);
                newlist = newlist.OrderByDescending(x => x.AddTime).Skip(size).Take(pagesize).ToList();
                page.list = newlist;
                return page;
            }
            else
            {
                if (edate != null)
                {
                    list = list.Where(x => x.AddTime >= Convert.ToDateTime(edate).AddDays(1)).ToList();
                }
                if (odate != null)
                {
                    list = list.Where(x => x.AddTime < Convert.ToDateTime(odate).AddDays(1)).ToList();
                }
                if (edate != null && odate != null)
                {
                    list = list.Where(x => x.AddTime < Convert.ToDateTime(odate).AddDays(1) && x.AddTime >= Convert.ToDateTime(edate).AddDays(1)).ToList();
                }
                if (uname != null)
                {
                    list = list.Where(x => x.UserName.Contains(uname)).ToList();
                }
                if (state != null)
                {
                    list = list.Where(x => x.State == state).ToList();
                }
                var page = new AskPageDto();
                page.pagecount = list.Count();
                page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / pagesize);
                list = list.OrderByDescending(x => x.AddTime).Skip(size).Take(pagesize).ToList();
                page.list = list.ToList();
                return page;
            }
        }

        public async Task<Askforleave> SetASKAll(int id)
        {
            return await aflRep.GetModelAsync(id);
        }
    }
}
